#include "i_key.h"
#include "STC89C5xRC.H"
#include "uart.h"
#include "lcd1602.h"
#include "task.h"

#define KEY_NUM 8
static KEY g_key[8];
static KEY_MSG g_key_msg;

sbit KEY_0 = P3 ^ 0;
sbit KEY_1 = P3 ^ 1;
sbit KEY_2 = P3 ^ 2;
sbit KEY_3 = P3 ^ 3;
sbit KEY_4 = P3 ^ 4;
sbit KEY_5 = P3 ^ 5;
sbit KEY_6 = P3 ^ 6;
sbit KEY_7 = P3 ^ 7;

/**
 * @brief key_msg_struct_panel
 *
 * @return *KEY_MSG
 */
KEY_MSG *key_msg_struct_panel(void)
{
    return &g_key_msg;
}

/**
 * @brief key_struct_panel
 *
 * @return *KEY
 */
KEY *key_struct_panel(void)
{
    return &g_key;
}

/**
 * @brief get_key_val
 *
 * @param ch
 * @return true
 * @return false
 */
static bool get_key_val(uint8_t ch)
{
    switch (ch)
    {
#if !UART_USED
    case 0:
        if (KEY_0 == 0)
            return 0;
        else
            return 1;

    case 1:
        if (KEY_1 == 0)
            return 0;
        else
            return 1;
#endif
    case 2:
        if (KEY_2 == 0)
            return 0;
        else
            return 1;
    case 3:
        if (KEY_3 == 0)
            return 0;
        else
            return 1;
    case 4:
        if (KEY_4 == 0)
            return 0;
        else
            return 1;
    case 5:
        if (KEY_5 == 0)
            return 0;
        else
            return 1;
    case 6:
        if (KEY_6 == 0)
            return 0;
        else
            return 1;
    case 7:
        if (KEY_7 == 0)
            return 0;
        else
            return 1;
    default:
        return RELEASED;
    }
}

/**
 * @brief key_scan
 *
 * @param key_msg -- key's msg
 * @param key -- key total num
 */
void key_scan(KEY_MSG *key_msg, KEY *key)
{
    uint8_t i;

#if !UART_USED
    for (i = 0; i < 8; ++i)
#else
    for (i = 2; i < 8; ++i)
#endif
    {
        key[i].val = get_key_val(i);       /* get val */
        if (key[i].last_val != key[i].val) /* check */
        {
            key[i].last_val = key[i].val; /* updata key status */
            if (key[i].val == 0)
            {
                key_msg->id = i;
                key_msg->pressed = 1;
            }
        }
    }
}

/**
 * @brief key_test
 *
 * @param key_msg key's msg
 */
void key_demo(void)
{
    if (key_msg_struct_panel()->pressed)
    {
        key_msg_struct_panel()->pressed = 0; /* clear key_msg status */
        switch (key_msg_struct_panel()->id)
        {
#if !UART_USED
        case 0:
            printf("P30 pressed\r\n");
            lcd1602_display_one_num(0, 0, 0);
            P1 = 0xFE;
            break;
        case 1:
            printf("P31 pressed\r\n");
            lcd1602_display_one_num(0, 0, 1);
            P1 = 0xFD;
            break;
#endif
        case 2:
            printf("P32 pressed\r\n");
            lcd1602_display_one_num(0, 0, 2);
            P1 = 0xFB;
            break;
        case 3:
            printf("P33 pressed\r\n");
            lcd1602_display_one_num(0, 0, 3);
            P1 = 0xF7;
            break;
        case 4:
            printf("P34 pressed\r\n");
            lcd1602_display_one_num(0, 0, 4);
            P1 = 0xEF;
            break;
        case 5:
            printf("P35 pressed\r\n");
            lcd1602_display_one_num(0, 0, 5);
            P1 = 0xDF;
            break;
        case 6:
            printf("P36 pressed\r\n");
            lcd1602_display_one_num(0, 0, 6);
            P1 = 0xBF;
            break;
        case 7:
            printf("P37 pressed\r\n");
            lcd1602_display_one_num(0, 0, 7);
            P1 = 0x7F;
            break;
        default:
            break;
        }
    }
}

void i_key_scan_demo(void)
{
    key_scan(key_msg_struct_panel(), key_struct_panel());
    key_demo();
}
